﻿using huqiang.Communication;
using huqiang.DataSet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace huqiang.ProtoDataPro
{
    public class TransitCmd
    {
        public const UInt16 IP = 0;
        public const UInt16 ID =1;
        public const UInt16 Result = 128;
    }
    public class TransitDataPro
    {
        public static void Handle(KcpUser link, UInt32 cmd, byte[] dat)
        {
            switch(cmd)
            {
                case TransitCmd.IP:
                    var ptrans= ProtoSerializer.Deserialize<IpTransit>(dat);
                    if(ptrans!=null)
                    {
                        var user = NetServer.Instance.FindLink(ptrans.ip,ptrans.port) as KcpUser ;
                        if(user==null)
                        {
                            link.Send(EnvelopeType.ProtoBuf,TransitCmd.Result,null,MsgType.Transit, 404);//未能找到目标设备
                        }
                        else
                        {
                            user.Send(EnvelopeType.ProtoBuf, ptrans.cmd, ptrans.data, ptrans.cmdType, ptrans.error);//将消息转发到目标设备
                        }
                    }
                    break;
                case TransitCmd.ID:
                    var dtrans = ProtoSerializer.Deserialize<IdTransit>(dat) ;
                    if (dtrans != null)
                    {
                        var user = NetServer.Instance.FindLink(dtrans.id)as KcpUser ;
                        if (user == null)
                        {
                            link.Send(EnvelopeType.ProtoBuf, TransitCmd.Result, null, MsgType.Transit, 404);//未能找到目标设备
                        }
                        else
                        {
                            user.Send(EnvelopeType.ProtoBuf, dtrans.cmd, dtrans.data, dtrans.cmdType, dtrans.error);//将消息转发到目标设备
                        }
                    }
                    break;
            }
        }
    }
}
